package com.yanqu.road.server.cmd.activity.dragonboatrace;

import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.pb.dragonboatrace.DragonBoatRaceProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.dragonboatrace.DragonBoatRaceModule;
import com.yanqu.road.server.gameplayer.module.player.BagModule;
import com.yanqu.road.server.manger.activity.dragonboatrace.DragonBoatRaceMgr;
import com.yanqu.road.server.manger.system.CommonMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.date.DateHelper;

@Cmd(code = GameProtocol.S_DRAGONBOATRACE_USE_PROP, desc = "赛龙舟使用道具")
public class DragonBoatRaceUsePropCmd extends AbstractCommand {


    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        DragonBoatRaceProto.DragonBoatRaceUsePropReqMsg reqMsg = DragonBoatRaceProto.DragonBoatRaceUsePropReqMsg.parseFrom(packet.getBytes());
        ret = DragonBoatRaceMgr.getActivityEndStatus(player);
        if(ret != 0){
            return;
        }
        DragonBoatRaceModule dragonBoatRaceModule = player.getModule(DragonBoatRaceModule.class);
        if(dragonBoatRaceModule.dragonBoatRaceUser == null || dragonBoatRaceModule.dragonBoatRaceUser.startTime <= 0){
            ret = GameErrorCode.E_DRAGONBOATRACE_NOT_JOIN_GAME;
            return;
        }
        long curTime = System.currentTimeMillis();
        if(dragonBoatRaceModule.dragonBoatRaceUser.curCalTime >= reqMsg.getTime() && reqMsg.getTime() > curTime){
            ret = GameErrorCode.E_DRAGONBOATRACE_TIME_ERROR;
            return;
        }
        if (reqMsg.getTime() >= dragonBoatRaceModule.dragonBoatRaceUser.endTime){
            ret = GameErrorCode.E_DRAGONBOATRACE_THE_GAME_IS_OVER;
            return;
        }
        if(!DragonBoatRaceMgr.getConfig().getDRAGONBOATRACE_ENERGY_ITEM_LIST().contains(reqMsg.getPropId())){
            ret = GameErrorCode.E_COMMON_ACTIVITY_NO_ACTIVITY_GOODS;
            return;
        }
        GoodsInfo goodsInfo = CommonMgr.getGoodsInfo(reqMsg.getPropId());
        if (goodsInfo == null) {
            ret = GameErrorCode.E_DRAGONBOATRACE_PROP_CONFIG_NO_FOUND;
            return;
        }
        // 判断cd
        long lastTime = dragonBoatRaceModule.dragonBoatRaceUser.usePropCdMap.getOrDefault(goodsInfo.getGoodsId(),0L);
        if(reqMsg.getTime() - lastTime < (goodsInfo.getParamList().get(2).intValue()* DateHelper.SECOND_MILLIONS)){
            ret = GameErrorCode.E_DRAGONBOATRACE_ACTIVITY_CD;
            return;
        }
        if(!DragonBoatRaceMgr.getConfig().getDRAGONBOATRACE_FREE_ENERGY_ITEM_LIST().contains(reqMsg.getPropId())){
            // 付费道具
            // 判断道具数量
            Integer num = dragonBoatRaceModule.dragonBoatRaceUser.usePropMap.getOrDefault(goodsInfo.getGoodsId(),0);
            int needNum = num + 1;
            BagModule bagModule = player.getModule(BagModule.class);
            if(!bagModule.hasEnoughGoods(goodsInfo.getGoodsId(), needNum)){
                ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
                return;
            }
            dragonBoatRaceModule.dragonBoatRaceUser.usePropMap.put(goodsInfo.getGoodsId(), needNum);
        }else {
            // 免费道具
            int oldNum = dragonBoatRaceModule.dragonBoatRaceUser.useFreePropMap.getOrDefault(goodsInfo.getGoodsId(),0);
            dragonBoatRaceModule.dragonBoatRaceUser.useFreePropMap.put(goodsInfo.getGoodsId(), oldNum + 1);
        }
        dragonBoatRaceModule.dragonBoatRaceUser.usePropCdMap.put(goodsInfo.getGoodsId(),reqMsg.getTime());
        dragonBoatRaceModule.createNodeStatus(0,goodsInfo.getParamList().get(0).intValue(),reqMsg.getTime(),goodsInfo.getParamList().get(1).intValue(),true);
        dragonBoatRaceModule.refreshGame(reqMsg.getTime());
        DragonBoatRaceProto.DragonBoatRaceClickDrumRespMsg.Builder builder = DragonBoatRaceProto.DragonBoatRaceClickDrumRespMsg.newBuilder();
        builder.setRet(0);
        builder.setNodeInfo(DragonBoatRaceMgr.builderDragonBoatRaceNodeInfoEntity(dragonBoatRaceModule.dragonBoatRaceUser));
        player.sendPacket(ClientProtocol.U_DRAGONBOATRACE_USE_PROP, builder);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            DragonBoatRaceProto.DragonBoatRaceClickDrumRespMsg.Builder builder = DragonBoatRaceProto.DragonBoatRaceClickDrumRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_DRAGONBOATRACE_USE_PROP, builder);
        }
    }
}
